<?php

namespace app\shop\model\welfare;

use app\common\model\BaseModel;
use app\shop\model\user\User;

class Welfares extends BaseModel
{
    protected $name = 'welfare';
    public function image()
    {
        return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'thumb');
    }

    public function welfare(){
        return $this->hasMany(Welfare::Class,'type');
    }

    public function getList($params)
    {

        //dump($where);die;
        return $this->with(['image'])
            ->where(array('is_delete'=>0))
            ->paginate($params, false, [
                'query' => request()->request()
            ]);
    }

    /**
     * 新增记录
     */
    public function add($data)
    {
        if (empty($data['thumb'])) {
            $this->error = '请上传图片';
            return false;
        }
        $data['app_id'] = self::$app_id;
        return $this->save($data);
    }

    /**
     * 更新记录
     */
    public function edit($data)
    {
        if (empty($data['thumb'])) {
            $this->error = '请上传图片';
            return false;
        }
        return $this->save($data);
    }

    public static function detail($id)
    {
        return self::with(['image'])->find($id);
    }

    public function get_count()
    {
        return self::where(array('is_delete'=>0))->count();
    }


}